// HSS.Forms.Docking.DockWindow.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DockWindow.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms.Docking
{
	#region Using Directives
	using System.ComponentModel;
	using System.Drawing;
	using System.Windows.Forms;
	#endregion

	#region DockWindow
	/// <summary>
	/// DockWindow
	/// </summary>
	[ToolboxItem(false)]
	public partial class DockWindow : Panel, INestedPanesContainer, ISplitterDragSource
	{
		#region Fields
		SplitterControl _splitter;
		#endregion

		#region Constructor
		internal DockWindow(DockPanel dockPanel, DockState dockState)
		{
			_nestedPanes = new NestedPaneCollection(this);
			_dockPanel = dockPanel;
			_dockState = dockState;
			Visible = false;

			SuspendLayout();

			if (DockState == DockState.DockLeft || DockState == DockState.DockRight ||
				DockState == DockState.DockTop || DockState == DockState.DockBottom)
			{
				_splitter = new SplitterControl();
				Controls.Add(_splitter);
			}

			if (DockState == DockState.DockLeft)
			{
				Dock = DockStyle.Left;
				_splitter.Dock = DockStyle.Right;
			}
			else if (DockState == DockState.DockRight)
			{
				Dock = DockStyle.Right;
				_splitter.Dock = DockStyle.Left;
			}
			else if (DockState == DockState.DockTop)
			{
				Dock = DockStyle.Top;
				_splitter.Dock = DockStyle.Bottom;
			}
			else if (DockState == DockState.DockBottom)
			{
				Dock = DockStyle.Bottom;
				_splitter.Dock = DockStyle.Top;
			}
			else if (DockState == DockState.Document)
			{
				Dock = DockStyle.Fill;
			}

			ResumeLayout();
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets the visible nested panes
		/// </summary>
		public VisibleNestedPaneCollection VisibleNestedPanes
		{
			get { return NestedPanes.VisibleNestedPanes; }
		}
		/// <summary>
		/// Gets the nested panes
		/// </summary>
		public NestedPaneCollection NestedPanes
		{
			get { return _nestedPanes; }
		} NestedPaneCollection _nestedPanes;
		/// <summary>
		/// Gets the DockPanel
		/// </summary>
		public DockPanel DockPanel
		{
			get { return _dockPanel; }
		} DockPanel _dockPanel;
		/// <summary>
		/// Gets the DockState
		/// </summary>
		public DockState DockState
		{
			get { return _dockState; }
		} DockState _dockState;
		/// <summary>
		/// Gets if this window is a Float Window
		/// </summary>
		public bool IsFloat
		{
			get { return DockState == DockState.Float; }
		}
		/// <summary>
		/// Gets the displaying rectangle
		/// </summary>
		public virtual Rectangle DisplayingRectangle
		{
			get
			{
				Rectangle rect = ClientRectangle;
				// if DockWindow is document, exclude the border
				if (DockState == DockState.Document)
				{
					rect.X += 1;
					rect.Y += 1;
					rect.Width -= 2;
					rect.Height -= 2;
				}
				// exclude the splitter
				else if (DockState == DockState.DockLeft)
					rect.Width -= Measures.SplitterSize;
				else if (DockState == DockState.DockRight)
				{
					rect.X += Measures.SplitterSize;
					rect.Width -= Measures.SplitterSize;
				}
				else if (DockState == DockState.DockTop)
					rect.Height -= Measures.SplitterSize;
				else if (DockState == DockState.DockBottom)
				{
					rect.Y += Measures.SplitterSize;
					rect.Height -= Measures.SplitterSize;
				}

				return rect;
			}
		}

		internal DockPane DefaultPane
		{
			get { return VisibleNestedPanes.Count == 0 ? null : VisibleNestedPanes[0]; }
		}

		#endregion

		#region Overrides
		/// <summary>
		/// 
		/// </summary>
		/// <param name="levent">A System.Windows.Forms.LayoutEventArgs that contains the event data</param>
		protected override void OnLayout(LayoutEventArgs levent)
		{
			VisibleNestedPanes.Refresh();
			if (VisibleNestedPanes.Count == 0)
			{
				if (Visible)
					Visible = false;
			}
			else if (!Visible)
			{
				Visible = true;
				VisibleNestedPanes.Refresh();
			}

			base.OnLayout(levent);
		}
		#endregion

		#region ISplitterDragSource Members

		void ISplitterDragSource.BeginDrag(Rectangle rectSplitter)
		{
		}

		void ISplitterDragSource.EndDrag()
		{
		}

		bool ISplitterDragSource.IsVertical
		{
			get { return (DockState == DockState.DockLeft || DockState == DockState.DockRight); }
		}

		Rectangle ISplitterDragSource.DragLimitBounds
		{
			get
			{
				Rectangle rectLimit = DockPanel.DockArea;
				Point location;
				if ((Control.ModifierKeys & Keys.Shift) == 0)
					location = Location;
				else
					location = DockPanel.DockArea.Location;

				if (((ISplitterDragSource)this).IsVertical)
				{
					rectLimit.X += MeasurePane.MinSize;
					rectLimit.Width -= 2 * MeasurePane.MinSize;
					rectLimit.Y = location.Y;
					if ((Control.ModifierKeys & Keys.Shift) == 0)
						rectLimit.Height = Height;
				}
				else
				{
					rectLimit.Y += MeasurePane.MinSize;
					rectLimit.Height -= 2 * MeasurePane.MinSize;
					rectLimit.X = location.X;
					if ((Control.ModifierKeys & Keys.Shift) == 0)
						rectLimit.Width = Width;
				}

				return DockPanel.RectangleToScreen(rectLimit);
			}
		}

		void ISplitterDragSource.MoveSplitter(int offset)
		{
			if ((Control.ModifierKeys & Keys.Shift) != 0)
				SendToBack();

			Rectangle rectDockArea = DockPanel.DockArea;
			if (DockState == DockState.DockLeft && rectDockArea.Width > 0)
			{
				if (DockPanel.DockLeftPortion > 1)
					DockPanel.DockLeftPortion = Width + offset;
				else
					DockPanel.DockLeftPortion += ((double)offset) / (double)rectDockArea.Width;
			}
			else if (DockState == DockState.DockRight && rectDockArea.Width > 0)
			{
				if (DockPanel.DockRightPortion > 1)
					DockPanel.DockRightPortion = Width - offset;
				else
					DockPanel.DockRightPortion -= ((double)offset) / (double)rectDockArea.Width;
			}
			else if (DockState == DockState.DockBottom && rectDockArea.Height > 0)
			{
				if (DockPanel.DockBottomPortion > 1)
					DockPanel.DockBottomPortion = Height - offset;
				else
					DockPanel.DockBottomPortion -= ((double)offset) / (double)rectDockArea.Height;
			}
			else if (DockState == DockState.DockTop && rectDockArea.Height > 0)
			{
				if (DockPanel.DockTopPortion > 1)
					DockPanel.DockTopPortion = Height + offset;
				else
					DockPanel.DockTopPortion += ((double)offset) / (double)rectDockArea.Height;
			}
		}

		#region IDragSource Members

		Control IDragSource.DragControl
		{
			get { return this; }
		}

		#endregion
		#endregion
	}
	#endregion
}